package cn.cocho.module.common.web.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 文件操作
 */
class ProjectFactory {


    public static final String FILE_LIST_KEY = "FILE_LIST_KEY";//存放文件列表集合的Key，不需要改动
    public static final String FOLDER_LIST_KEY = "FOLDER_LIST_KEY";//存放目录列表集合的Key，不需要改动

    /**
     * 列出该路径下所有文件及目录，包括子目录的文件
     *
     * @param path
     * @return
     */
    public static Hashtable<String, List<String>> filesMap = new Hashtable<String, List<String>>();

    /**
     * 初始化文件列表
     *
     * @param path
     */
    public static void initFilesMap(String path) {
        List<String> fileList = new ArrayList<String>();
        List<String> forderList = new ArrayList<String>();
        filesMap.put(FILE_LIST_KEY, fileList);
        filesMap.put(FOLDER_LIST_KEY, forderList);
        getFileList(path);
    }

    public static void getFileList(String path) {
        File sourceFolder = new File(path);
        String[] files = sourceFolder.list();
        File temp = null;
        if (files != null) {
            for (String filePath : files) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + filePath);
                } else {
                    temp = new File(path + File.separator + filePath);
                }
                String tempPath = temp.getAbsolutePath();
                if (temp.isFile() && isScanFileType(tempPath)) {
                    filesMap.get(FILE_LIST_KEY).add(tempPath);
                } else {
                    filesMap.get(FOLDER_LIST_KEY).add(tempPath);
                    getFileList(tempPath);
                }
            }
        }
    }

    public static boolean isScanFileType(String filePath){
         for(String type : Config.scanFileTypes){
            if(filePath.endsWith(type)){
                return true;
            }
         }
        return  false;
    }


    /**
     * 替换文件内容
     */
    public static void replaceContent() {
        List<String> fileList = filesMap.get(FILE_LIST_KEY);
        if (fileList != null) {
            for (String path : fileList) {
                File temp = new File(path);
                if (temp.exists()) {
                    String content = FileUtils.readFile(path);
                    String newContent = replaceContent(content);
                    if (!content.equals(newContent)) {
                        FileUtils.writeFile(path, newContent);
                        System.out.println("内容被修改的文件：" + path);
                    }
                }
            }
        }
    }

    /**
     * 修改文件名称
     */
    public static void renameFile() {
        List<String> fileList = filesMap.get(FILE_LIST_KEY);
        for (String path : fileList) {
            File temp = new File(path);
            if (temp.exists()) {
                String fileName = temp.getName();
                String newFileName = replaceContent(fileName);
                if (!fileName.equals(newFileName)) {
                    File newFile = new File(temp.getParent() + File.separator + newFileName);
                    temp.renameTo(newFile);
                    System.out.println("将目录名称[" + temp.getAbsolutePath() + "]修改为[" + newFile.getAbsolutePath() + "]");
                }
            }
        }
    }

    /**
     * 修改目录名称
     */
    public static void renameForder(String path) {
        File sourceFolder = new File(path);
        String[] files = sourceFolder.list();
        File temp = null;
        for (String filePath : files) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + filePath);
            } else {
                temp = new File(path + File.separator + filePath);
            }
            String tempPath = temp.getAbsolutePath();
            if (temp.isDirectory()) {
                renameForder(tempPath);
                String forderName = temp.getName();
                String newForderName = replaceContent(forderName);
                if(!forderName.equals(newForderName)){
                    File newForder = new File(temp.getParent() + File.separator + newForderName);
                    temp.renameTo(newForder);
                    System.out.println("将目录名称[" + temp.getAbsolutePath() + "]修改为[" + newForder.getAbsolutePath() + "]");

                }
            }
        }
    }

    private static String replaceContent(String content) {
        String newContent = content.replace(Config.renameByPackageName1, Config.renameToPackageName1);
        newContent = newContent.replace(Config.renameByPackageName2, Config.renameToPackageName2);
        newContent = newContent.replace(Config.renameByPackageName3, Config.renameToPackageName3);
        newContent = newContent.replace(Config.renameByAppName, Config.renameToAppName);
        return newContent;
    }

    /**
     * 清理指定文件
     */
    public static void cleanFile() {
        List<String> fileList = filesMap.get(FILE_LIST_KEY);
        for (String path : fileList) {
            File temp = new File(path);
            String fileName = temp.getName();
            boolean delete = false;
            if (fileName.startsWith(Config.DEL_POINT)) {
                delete = true;
            }
            if (fileName.endsWith(Config.DEL_END_WITH_IML)) {
                delete = true;
            }

            if (fileName.equalsIgnoreCase(Config.DEL_IGNORED_GITIGNORE)) {
                delete = false;
            }
            if (delete) {
                temp.delete();
                System.out.println("删除文件：" + path);
            }

        }

    }

    /**
     * 清理指定文件夹
     */
    public static void cleanFolder() {
        List<String> forderList = filesMap.get(FOLDER_LIST_KEY);
        for (String path : forderList) {
            boolean delete = false;
            File temp = new File(path);
            String forderName = temp.getName();
            if (forderName.equalsIgnoreCase(Config.DEL_FOLDER_TARGET)) {// 清理掉target目录及其子目录下的文件
                delete = true;
            } else if (forderName.equalsIgnoreCase(Config.DEL_FOLDER_IDEA)) {// 清理掉.idea目录及其子目录下的文件
                delete = true;
            }

            if (delete) {
                FileUtils.deleteForder(temp);
            }
        }
    }


}
